package com.demo.samples.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {

    private static final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    @GetMapping("/getUserId")
    public Map<String, Object> getUserId(@RequestParam("userId") Integer userId) {
        // 从threadLocal获取用户信息
        String s = Thread.currentThread().getName() + ": 用户id:" + threadLocal.get();
        // 设置用户信息
        threadLocal.set(userId);
        // 再次threadLocal获取用户信息
        String s1 = Thread.currentThread().getName() + ": 用户id:" + threadLocal.get();
        Map<String, Object> result = new HashMap<>();
        result.put("s", s);
        result.put("s1", s1);
        threadLocal.remove();
        // 返回两次结果
        return result;
    }

}
